package com.gzhryc.shared_device.oem.micro.api.controllers.xcx.v1.cdb;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.cdb.code.CDBCostConfig;
import com.gzhryc.shared_device.cdb.code.dao.db.BatteryCabinet;
import com.gzhryc.shared_device.cdb.code.dao.db.CDBFeeTemplate;
import com.gzhryc.shared_device.cdb.code.dao.enums.BatteryCabinetEnum;
import com.gzhryc.shared_device.cdb.code.dao.enums.ECDBCostMode;
import com.gzhryc.shared_device.cdb.code.services.BatteryCabinetService;
import com.gzhryc.shared_device.cdb.code.services.CDBFeeTemplateService;
import com.gzhryc.shared_device.common.device.dao.db.Device;
import com.gzhryc.shared_device.common.device.services.DeviceService;
import com.gzhryc.shared_device.oem.code.ConfigUtils;
import com.gzhryc.shared_device.oem.code.device.EDeviceFunCode;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceBusinessConfig;
import com.gzhryc.shared_device.oem.code.place.services.PlaceBusinessConfigService;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.cdb.CDBScanGetRequest;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.cdb.CDBScanGetResponse;

public class CDBScanController {

    static Logger log = Logger.getLogger(CDBScanController.class);

    @Route(value = { "/xcx/v1/cdb/scan/get.do" }, method = "POST")
    public CDBScanGetResponse getSharedDevice(HttpServletRequest request) {
        CDBScanGetResponse apiResp = new CDBScanGetResponse(ClientAPICodeConstants.NULL_DATA, "未找到设备信息");
        CDBScanGetRequest apiReq = WebUtils.getJsonRequest(request, CDBScanGetRequest.class);
        if (apiReq != null && StringUtils.isNotBlank(apiReq.getBody().getDeviceSn())) {
            String deviceSn = apiReq.getBody().getDeviceSn().trim();
            Device device = DeviceService.self().getByDeviceSn(deviceSn);
            if (device == null || device.getPlaceId() == null) {
                log.error("未找到{{0}}设备或未部署",deviceSn);
                return apiResp;
            }

            BatteryCabinet batteryCabinet = BatteryCabinetService.self().getByDeviceSn(deviceSn);
            if (batteryCabinet == null) {
                log.error("未找到{{0}}设备",deviceSn);
                return apiResp;
            }

            apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
            apiResp.getBody().setDeviceSn(device.getDeviceSn());
            if(BatteryCabinetEnum.State.Online.index().equals(batteryCabinet.getState())) {
                //TODO 检查充电饱和状态
                apiResp.getBody().setState(1);  //在线
            }else{
                apiResp.getBody().setState(2);  //离线
                return apiResp;
            }
            
            CDBCostConfig cdbConfig = ConfigUtils.getCDBCostConfig();
            if(cdbConfig.getWxPreAuthPay()) {
            	apiResp.getBody().setCostMode(ECDBCostMode.ExemptionDeposit.index());
            }else {
            	apiResp.getBody().setCostMode(ECDBCostMode.Deposit.index());
            }

            CDBFeeTemplate feeTemplate = null;
            PlaceBusinessConfig deviceFunc = PlaceBusinessConfigService.self().getByKeys(device.getPlaceId(), EDeviceFunCode.CDB.index());
            if(deviceFunc != null && deviceFunc.getFeeTemplateId() != null) {
                feeTemplate = CDBFeeTemplateService.self().getById(deviceFunc.getFeeTemplateId());
            }

            if(feeTemplate == null){
                CDBCostConfig costConfig = ConfigUtils.getCDBCostConfig();
                feeTemplate = CDBFeeTemplateService.buildByDefault(costConfig);
            }

            apiResp.getBody().setDepositMoneyStr(feeTemplate.getDepositMoneyStr());
            apiResp.getBody().setDayMaxMoneyStr(feeTemplate.getDayMaxMoneyStr());
            apiResp.getBody().setPayIntervalMoneyStr(feeTemplate.getPayIntervalMoneyStr());
            apiResp.getBody().setFreeTimes(feeTemplate.getFreeTimes());
        }
        return apiResp;
    }
}
